package com.cc2019.buke.user.Activity;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.cc2019.buke.R;
import com.cc2019.buke.user.tools.Constants;
import com.cc2019.buke.user.tools.DataBaseHelper;

public class MyInfoActivity extends AppCompatActivity {
    private static final String TAG = "======";
    private Button changepassword;
    private Button changeemail;
    private Intent intent;
    private String NAME;
    private DataBaseHelper DBH;
    private Button see;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_info);
        changepassword=(Button) findViewById(R.id.change_password);
        changeemail=(Button) findViewById(R.id.change_email);
        DBH=DataBaseHelper.getInstance(this);
        see=(Button)findViewById(R.id.see_info);
        intent = getIntent();
        //之前出错是只获取了上一个页面传递过来的信息  但是 ,上一个页面没有传。。。。
        NAME=intent.getStringExtra("username");//获取传入的姓名查询信息
        Log.e(TAG, "onCreate: "+NAME );
        
        see.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder=new AlertDialog.Builder(MyInfoActivity.this);
                builder.setTitle("个人信息");

                StringBuilder stringBuilder=new StringBuilder();
                SQLiteDatabase sqLiteDatabase=DBH.getWritableDatabase();

                Cursor cursor=sqLiteDatabase.query(Constants.TABLE_NAME,null,"name=?", new String[]{NAME},null,null,null);

                while (cursor.moveToNext()){
                    String id=cursor.getString(cursor.getColumnIndex("id"));
                    String name=cursor.getString(cursor.getColumnIndex("name"));
                    String password=cursor.getString(cursor.getColumnIndex("password"));
                    String email=cursor.getString(cursor.getColumnIndex("email"));

                    stringBuilder.append("id: "+id+"\n");
                    stringBuilder.append("姓名: "+name+"\n");
                    stringBuilder.append("密码: "+password+"\n");
                    stringBuilder.append("邮箱: "+email+"\n");
                }

                cursor.close();
                builder.setMessage(stringBuilder.toString());
                builder.create().show();

            }
        });

        changepassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder=new AlertDialog.Builder(MyInfoActivity.this);
                LayoutInflater factory=LayoutInflater.from(MyInfoActivity.this);
                final View view=factory.inflate(R.layout.user_change_password,null);
                builder.setView(view);
                builder.setTitle("修改密码");
                builder.setNegativeButton("取消", null);

                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

                    EditText firstPassword = (EditText) view.findViewById(R.id.user_change_password);
                    EditText secondPassword = (EditText) view.findViewById(R.id.user_change_password_second_password);

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String first = firstPassword.getText().toString();
                        String second = secondPassword.getText().toString();
                        if (!TextUtils.isEmpty(first) && !TextUtils.isEmpty(second)) {
                            if (second.equals(first)) {

                                SQLiteDatabase db = DBH.getWritableDatabase();
//                                db.beginTransaction();
                                ContentValues values=new ContentValues();
                                values.put("password",second);
                                db.update(Constants.TABLE_NAME,values,"name=?",new String[]{NAME});

//                                String existSql = " update "+Constants.TABLE_NAME+" set password=? where name=?";
//                                db.execSQL(existSql,new String[]{second,NAME});
//                                db.execSQL("update usermessage set password=? where id=?", new String[]{second, ID});
                                Toast.makeText(MyInfoActivity.this, "密码修改成功", Toast.LENGTH_SHORT).show();
//                                db.endTransaction();
                                db.close();
                            } else {
                                Toast.makeText(MyInfoActivity.this, "两次密码不一致！", Toast.LENGTH_SHORT).show();
                            }
                        } else {
                            Toast.makeText(MyInfoActivity.this, "密码不能为空~", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                builder.create().show();
            }

        });

        changeemail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder=new AlertDialog.Builder(MyInfoActivity.this);
                LayoutInflater factory=LayoutInflater.from(MyInfoActivity.this);
                final View view=factory.inflate(R.layout.user_change_email,null);
                builder.setView(view);
                builder.setTitle("修改密码");
                builder.setNegativeButton("取消", null);

                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

                    EditText oldEmail = (EditText) view.findViewById(R.id.user_change_email_old);
                    EditText newEmail = (EditText) view.findViewById(R.id.user_change_email_new);

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String oldE = oldEmail.getText().toString();
                        String newE = newEmail.getText().toString();
                        if (!TextUtils.isEmpty(oldE) && !TextUtils.isEmpty(newE)) {
                            if (newE.equals(oldE)) {
                                Toast.makeText(MyInfoActivity.this, "新旧邮箱地址一致！！！", Toast.LENGTH_SHORT).show();


                            } else {
                                SQLiteDatabase db = DBH.getWritableDatabase();
//                                db.beginTransaction();
                                ContentValues values=new ContentValues();
                                values.put("email",newE);
                                db.update(Constants.TABLE_NAME,values,"name=?",new String[]{NAME});

//                                String existSql = " update "+Constants.TABLE_NAME+" set password=? where name=?";
//                                db.execSQL(existSql,new String[]{second,NAME});
//                                db.execSQL("update usermessage set password=? where id=?", new String[]{second, ID});
                                Toast.makeText(MyInfoActivity.this, "邮箱地址修改成功", Toast.LENGTH_SHORT).show();
//                                db.endTransaction();
                                db.close();
                            }
                        } else {
                            Toast.makeText(MyInfoActivity.this, "两次邮箱地址均不能为空~", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                builder.create().show();
            }

        });

    }
}